package controller

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"shangke-device/app/http/request"
	"shangke-device/app/svcContent"
	"shangke-device/internal/result"
	"shangke-device/internal/task"
	"time"
)

type Device struct {
	svc *svcContent.SvcContent
}

func (d Device) Open() gin.HandlerFunc {
	return func(c *gin.Context) {
		fmt.Sprintln("open")
		var req request.DeviceOpenRequest
		err := c.ShouldBind(&req)
		if err != nil {
			result.JsonResult(c, nil, err)
			return
		}

		t := task.NewTask().Add("test1", func(t *task.Task) (v interface{}, e error) {
			v = map[string]string{
				"test": "ok",
			}
			time.Sleep(2 * time.Second)
			fmt.Println("test1 执行完成...")
			return
		}).Add("test2", func(t *task.Task) (v interface{}, e error) {
			v = map[string]string{
				"test": "ok",
			}

			fmt.Println("test2 执行完成...")
			return
		}).Wait()

		err = t.Err()
		result.JsonResult(c, nil, err)
	}
}

func NewDevice(svc *svcContent.SvcContent) Device {
	return Device{
		svc: svc,
	}
}
